from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from xinan_ui.tools.App_driver import AppDriver01
import logging


class BasePage():
    def __init__(self,driver):
        self.driver=driver


    #定位元素
    def  get_element(self,local=()):
        try:
            we=WebDriverWait(self.driver,7).until(EC.presence_of_element_located((getattr(By,local[0]),local[1])),message='元素未找到')

        except Exception as e:
            print(e)
            logging.error(e)
        else:
            return we


class PageHandle():

    def __init__(self,driver):
        self.driver=driver
        self.x= BasePage(self.driver)
        window_size = self.driver.get_window_size()
        self.width = window_size['width']
        self.height = window_size['height']


    def input_element(self,local,text):
        self.x.get_element(local).send_keys(text)

    def clear_element(self,local):
        self.x.get_element(local).clear()
    def click_button(self,local):
        self.x.get_element(local).click()
    def get_text(self,local):
        return self.x.get_element(local).get_attribute("text")
    #向上滑动
    def slide_up(self):
        self.x.driver.swipe(0.5 * self.width, 0.75 * self.height, 0.5 * self.width, 0.2 * self.height, 200)
    #向下滑动
    def slide_down(self):
        self.x.driver.swipe(self.width, 0.1 * self.height, self.width, 0.9 * self.height, 200)

    #向左滑动
    def slide_left(self):
        driver = self.driver
        driver.swipe(0.8*self.width,0.35 *self.height,0.5* self.width,0.35 *self.height,200)


if __name__ == '__main__':
    F = PageHandle()
    F.x.driver
    loc=By.XPATH,"""/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.TextView[2]"""
    F.click_button(loc)